package com.hy.frame.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;


/**
 * title 可控制图标大小TextView，另外只支持drawableLeft居中
 * author heyan
 * time 19-7-10 下午4:24
 * desc 无
 */
public class QTextView extends AppCompatTextView implements IAutoDesign {

    protected DesignTextHelper designHelper;

    public QTextView(Context context) {
        this(context, null);
    }

    public QTextView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
    }

    public QTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs, defStyleAttr);
    }

    @SuppressLint("CustomViewStyleable")
    public void initAttrs(Context context, AttributeSet attrs, int defStyleAttr) {
        if (designHelper == null) {
            designHelper = new DesignTextHelper(this);
        }
        designHelper.loadAttributes(attrs, defStyleAttr);
        designHelper.loadBackgroundAttributes(attrs, defStyleAttr);
        designHelper.loadTextAttributes(attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        designHelper.onLayout(getWidth(), getHeight());
    }

    @Override
    public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        if (designHelper != null)
            designHelper.setCompoundDrawables(left, top, right, bottom);
        super.setCompoundDrawables(left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (designHelper != null)
            designHelper.onDraw(canvas);
        super.onDraw(canvas);
    }

    @Override
    public boolean isScaleEnabled() {
        return designHelper.scaleEnabled;
    }

    @Override
    public DesignHelper getDesignHelper() {
        return designHelper;
    }
}
